﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClientAppraise.ViewModels.Validator
{
    public sealed class ZxCompareAttribute : ValidationAttribute
    {
        string _otherField;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="otherField">属性，不是字段，是属性</param>
        public ZxCompareAttribute(string otherField)
        {
            _otherField = otherField;
        }

        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            var instance = validationContext.ObjectInstance;

            var instanceType = instance.GetType();
            var theFile = instanceType.GetProperty(_otherField);

            if (theFile == null)
            {
                return new($"没有找到指定字段{_otherField}");
            }

            var theValue = theFile.GetValue(instance);

            if (value?.ToString() != theValue?.ToString())
            {
                return new($"二次输入内容不相同");
            }

            return ValidationResult.Success;
        }
    }
}
